package com.sduept.nwld.dataserver.controller.library;

import com.esms.common.util.StringUtils;
import com.sduept.bigdata.fault.entity.ProtectionComponentLibrary;
import com.sduept.bigdata.fault.manager.ProtectionCompentCache;
import com.sduept.bigdata.fault.manager.ProtectionComponentLibraryManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @date 2018-11-23
 */
@Named
@ViewScoped
public class ProtectionComponentLibraryController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8572844884473259866L;
	
	@Autowired
	private ProtectionComponentLibraryManager protectionComponentLibraryManager;
	@Autowired
	private ProtectionCompentCache cache;
	
	private TreeNode root;
	private TreeNode currentSelect;//当前选择
	private List<TreeNode> nodeList = new ArrayList<TreeNode>();
	private List<ProtectionComponentLibrary> componentList = new ArrayList<ProtectionComponentLibrary>();//动作元件列表
	private ProtectionComponentLibrary selected;//当前选择元件
	private List<String> devTypeList=new ArrayList<String>();
	
	@PostConstruct
	public void init(){
		devTypeList=protectionComponentLibraryManager.listAllDevTypes();
		initTreeNode();
	}
	
	/**
	 * 初始化树节点
	 */
	private void initTreeNode(){
		nodeList.clear();
		root = new DefaultTreeNode();
		List<String> manuList = protectionComponentLibraryManager.getProtectionManufacturers();
		for(String manu : manuList){
			TreeNode treeNode = new LibTreeNode("manu", manu, root);
			nodeList.add(treeNode);
		}
	}
	
	/**
	 * 刷新按钮事件
	 */
	public void refresh(){
		initTreeNode();
		currentSelect=null;
		selected=null;
		componentList.clear();
	}
	/**
	 * 查询故障型号
	 * @param query
	 * @return
	 */
	public List<String> completeDevType(String query) {
		List<String> results = new ArrayList<String>();
		Set<String> sets=new HashSet<String>();
		for (String s : devTypeList){
			if(s.toUpperCase().startsWith(query.toUpperCase()))
				sets.add(s);
		}
		results.addAll(sets);
		return results;
	}
	
	public void locateDevType(SelectEvent event){
		Object item = event.getObject();
		String manufacturer = protectionComponentLibraryManager.getManufacturer(item.toString());
		if(StringUtils.isEmpty(manufacturer)){
			return ;
		}
		currentSelect=null;
		initTreeNode();
		for(TreeNode tmp:nodeList){
			if("manu".equals(tmp.getType())&&manufacturer.equals(tmp.getData().toString())){
				//tmp.getChildren().clear();
				List<String> modelList = protectionComponentLibraryManager.getProtectionType(manufacturer);
				for(String model : modelList){
					TreeNode treeNode = new DefaultTreeNode("model", model, tmp);
					nodeList.add(treeNode);
				}
				tmp.setExpanded(true);
				break;
			}
		}
		for(TreeNode node:nodeList){
			if("model".equals(node.getType())&&item.toString().equals(node.getData().toString())){
				node.setSelected(true);
				currentSelect=node;
				selected = null;//重置选择
				componentList = new ArrayList<ProtectionComponentLibrary>();//清空数据
				if("model".equals(currentSelect.getType())){//前台选择保护型号
					componentList = protectionComponentLibraryManager.listLibraries(currentSelect.getData().toString());
				}
				break;
			}
		}
	}
	/**
	 * 前台保护厂家展开
	 * @param event
	 */
	public void onNodeExpand(NodeExpandEvent event){
		currentSelect = null;//重置选择的节点
		TreeNode node = event.getTreeNode();
		List<String> modelList = protectionComponentLibraryManager.getProtectionType(node.getData().toString());
		for(String model : modelList){
			TreeNode treeNode = new DefaultTreeNode("model", model, node);
			nodeList.add(treeNode);
		}
	}
	
	/**
	 * 前台选择树节点
	 * @param event
	 */
	public void onNodeSelect(NodeSelectEvent event){
		selected = null;//重置选择
		componentList = new ArrayList<ProtectionComponentLibrary>();//清空数据
		currentSelect = event.getTreeNode();
		if("model".equals(currentSelect.getType())){//前台选择保护型号
			componentList = protectionComponentLibraryManager.listLibraries(currentSelect.getData().toString());
		}
	}
	
	/**
	 * 动作元件选择
	 * @param event
	 */
	public void onRowSelect(SelectEvent event) {
		selected = (ProtectionComponentLibrary) event.getObject();
	}
	
	/**
	 * 新建前操作
	 */
	public void preCreate(){
		selected = new ProtectionComponentLibrary();
		if(null!=currentSelect){
			selected.setProtectModel(currentSelect.getData().toString());
		}
	}
	
	/**
	 * 保存
	 */
	public void saveComponent(){
		if(null == selected){
			addErrorMessage("请输入动作元件信息！");
		}else{
			if(StringUtils.isEmpty(selected.getActionComponent())){
				addErrorMessage("请输入动作元件！");
			}else{
				selected=protectionComponentLibraryManager.save(selected);
				componentList = protectionComponentLibraryManager.listLibraries(currentSelect.getData().toString());
				cache.save2Map(selected);
				selected = null;
				addSuccessMessage("保存成功!");
			}
		}
	}
	
	/**
	 * 删除
	 */
	public void deleteComponent(){
		if(null == selected){
			addErrorMessage("请选择要删除的动作元件！");
		}else{
			protectionComponentLibraryManager.delete(selected.getId());
			componentList.remove(selected);
			int order=1;
			for(ProtectionComponentLibrary lib:componentList){
				if(selected.getRelationCondition().equals(lib.getRelationCondition())){
					lib.setActionOrder(order);
					protectionComponentLibraryManager.save(lib);
					order++;
				}
			}
			componentList.clear();
			componentList.addAll(protectionComponentLibraryManager.listLibraries(currentSelect.getData().toString()));
			cache.removeAndReload(selected);
			selected = null;
			addSuccessMessage("删除成功！");
			
		}
	}
	
	public void onRowReorder(ReorderEvent event){
		int fromIndex = event.getFromIndex();
		ProtectionComponentLibrary source = componentList.get(fromIndex);
		List<ProtectionComponentLibrary> compareAndSaveList = compareAndSave(source,componentList);
		cache.compareAndSave(compareAndSaveList,true);
	}
	
	
	private List<ProtectionComponentLibrary>  compareAndSave(ProtectionComponentLibrary source,List<ProtectionComponentLibrary> componentList ){
		List<ProtectionComponentLibrary> result = new ArrayList<ProtectionComponentLibrary>();
		int order=1;
		for(ProtectionComponentLibrary lib:componentList){
			if(source.getRelationCondition().equals(lib.getRelationCondition())){
				lib.setActionOrder(order);
				result.add(lib);
				order++;
			}
		}
		return result;
	}
	

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getCurrentSelect() {
		return currentSelect;
	}

	public void setCurrentSelect(TreeNode currentSelect) {
		this.currentSelect = currentSelect;
	}

	public List<TreeNode> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<TreeNode> nodeList) {
		this.nodeList = nodeList;
	}

	public List<ProtectionComponentLibrary> getComponentList() {
		return componentList;
	}

	public void setComponentList(List<ProtectionComponentLibrary> componentList) {
		this.componentList = componentList;
	}

	public ProtectionComponentLibrary getSelected() {
		return selected;
	}

	public void setSelected(ProtectionComponentLibrary selected) {
		this.selected = selected;
	}
	
}
